﻿using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Exceptions.RepositoryExceptions;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.InternalModels.NewsInformationModels;

namespace SQLRepositories.NewsRepositories
{
    public class PersonRepository : EntityBaseRepository<PersonModel>, IPersonRepository
    {
        public PersonRepository(IDBEconomicInformationServiceContext context):base(context)
        {}

        public void Create(PersonModel item)
        {
            Context.Persons.Add(item);
            Context.SaveChanges();
        }

        public PersonModel GetById(Guid id)
        {
            return Context.Persons.FirstOrDefault(x => x.Id.Equals(id));
        }

        public void Delete(Guid id)
        {
            var person = Context.Persons.FirstOrDefault(x => x.Id.Equals(id));
            if (person == null)
                throw new EntityNotFoundException("Person");
            Context.Persons.Remove(person);
            Context.SaveChanges();
        }

        public void Delete(PersonModel item)
        {
            Context.Persons.Remove(item);
            Context.SaveChanges();
        }

        public IEnumerable<PersonModel> GetAll()
        {
            return Context.Persons;
        }
    }
}
